<?php
namespace Admin\Controller;

class AderController extends Controller {

    public function index() {
        $m    = D2();
        $page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();

        $this->assign('page', $page)
             ->display();
    }

    public function modify() {
        $id = I('get.id', 0, 'intval');

        if (empty($id)) {
            throw new \Exception('参数不完整，无法获取数据');
        }

        $m = D('Ader');

        if (!$record = $m->getById($id)) {
            throw new \Exception('没有找到广告主信息'); //尝试着执行try{} 如果执行到throw new Exception则跳到catch{}里面执行
        }

        unset($record['passwd']);

        $this->assign('record', $record)
             ->display();
    }

    public function doSave() {
        $d = $_POST;
        $m = D('Ader');

        try {
            $r = $m->save2($d)
                 ? ['s' => true, 'm' => '保存成功']
                 : ['s' => false, 'm'=> '保存出错'];
        } catch(\Exception $e) {
            $r = ['s' => false, 'm' => $e->getMessage()];
        }

        showJson($r);
    }

    public function del() {
        $id = I('get.id', 0, 'intval');

        if (empty($id)) {
            showJson(['s' => false, 'm' => '参数不完整，无法删除数据']);
        }

        $m = D('Ader');
        $r = $m->del($id) 
             ? ['s' => true,  'm' => '删除成功']
             : ['s' => false, 'm' => '删除出错'];

        showJson($r);
    }

    /**
     * changeStatus
     * 更改审核状态
     *
     * @return void
     */
    public function changeStatus() {
        $id     = I('get.id',     0, 'intval');
        $status = I('get.status', 0, 'intval');

        try {
            $r = D('Ader')->changeStatus($id, $status)
                 ? ['s' => true,  'm' => '操作成功']
                 : ['s' => false, 'm' => '操作出错'];
        } catch(\Exception $e) {
            $r = ['s' => false, 'm' => $e->getMessage()];
        }

        showJson($r);
    }

}
